package com.ruoyi.framework.web.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

@Component
public class NoPasswordTokenProvider implements AuthenticationProvider {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        NoPasswordToken noPasswordToken = (NoPasswordToken) authentication;
        String loginName = (String) noPasswordToken.getPrincipal();
        UserDetails userDetails = userDetailsService.loadUserByUsername(loginName);
        return new NoPasswordToken(userDetails, userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return NoPasswordToken.class.isAssignableFrom(authentication);
    }
}
